package com.xinnet.mms.empcustomrelation.dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.xinnet.core.dao.impl.BaseDaoImpl;
import com.xinnet.mms.empcustomrelation.dao.EmpCustomRelationDao;
import com.xinnet.mms.entity.CustomStateHist;
import com.xinnet.mms.entity.Customer;
import com.xinnet.mms.entity.EmpCustomRelation;

@Repository("empCustomRelationDao")
public class EmpCustomRelationDaoImpl  extends BaseDaoImpl<EmpCustomRelation, Long> implements EmpCustomRelationDao{
	/**
	 * 插入人员客户关系历史
	 */
	@Override
	public void addEmpCustomRelation(EmpCustomRelation empCustomRelation) throws SQLException {
		this.getSqlSession().insert("EmpCustomRelation.addEmpCustomRelation",empCustomRelation);
	}
	
	/**
	 * 获取所有流转的客户
	 * @return List
	 */	public int getTransferCustomCount(Customer customer){
		Integer count = this.getSqlSession().selectOne("EmpCustomRelation.getTransferCustomCount",customer);
		return count;
	}
	
	/**
	 * 获取所有流转的客户
	 * @return List
	 */	public List<EmpCustomRelation> getTransferCustom(Customer customer){
		List<EmpCustomRelation> empCustomRelationList = this.getSqlSession().selectList("EmpCustomRelation.getTransferCustom",customer);
		return empCustomRelationList;
	}
	/**
	 * 删除EmpCustomRelation
	 * @param customId
	 * @param empNo
	 */
	@Override
	public void delEmpCustomRelationByCustomIdAndEmpNo(String customId,String empNo,String productCode) {
		EmpCustomRelation empCustomRelation = new EmpCustomRelation();
		empCustomRelation.setCustomId(customId);
		empCustomRelation.setEmpId(empNo);
		empCustomRelation.setProductCode(productCode);
		super.getSqlSession().delete("EmpCustomRelation.delEmpCustomRelationByCustomIdAndEmpNo", empCustomRelation);
	}

	@Override
	public void updateEmpCustomRelation(EmpCustomRelation empCustomRelation) {
		
		super.getSqlSession().update("EmpCustomRelation.updateEmpCustomRelation", empCustomRelation);
	}

	/**
	 * 用销售ID和用户ID查询是否接收
	 */
	@Override
	public Integer getAcceptCustomerCountByCusIdAndEmpno(Customer customer) throws SQLException {
		return this.getSqlSession().selectOne("EmpCustomRelation.getAcceptCustomerCountByCusIdAndEmpno",customer);
	}

	/**
	 * 处理调配修改人员客户关系
	 */
	@Override
	public void updateEmpCustomRelationByMix(Customer customer) throws SQLException {
		super.getSqlSession().update("EmpCustomRelation.updateEmpCustomRelationByMix", customer);
	}
	
	@Override
	public EmpCustomRelation getEmpCustomRelationById(String id) {
	
		return super.getSqlSession().selectOne("EmpCustomRelation.getEmpCustomRelationById", id);
	}
	
	/**
	 * 通过主键IDStr获取客户销售关系记录
	 * @param id
	 * @return
	 */
	public List<EmpCustomRelation> getEmpCustomRelationByIdStr(String idStr){
		return super.getSqlSession().selectList("EmpCustomRelation.getEmpCustomRelationByIdStr", idStr);
	}
	
	@Override
	public void updateEmpCustomRelationById(EmpCustomRelation empCustomRelation) {
		
		super.getSqlSession().update("EmpCustomRelation.updateEmpCustomRelationById", empCustomRelation);
	}
	
	/**
	 * 根据客户关系ID 删除客户关系表记录
	 */
	@Override
	public void delByEcrId(String ecrId) throws SQLException {
		this.getSqlSession().delete("EmpCustomRelation.delBydelByEcrId",ecrId);
	}
	
	/**
	 * 根据EmpId customId获取EmpCustomRelation
	 * @param empId
	 * @param customId
	 * @return
	 */
	public List<EmpCustomRelation> getEmpCustomRelationByEmpIdAndCustomId(String empId,String customId){
		EmpCustomRelation empCustomRelation = new EmpCustomRelation();
		empCustomRelation.setEmpId(empId);
		empCustomRelation.setCustomId(customId);
		return this.getSqlSession().selectList("EmpCustomRelation.getEmpCustomRelationByEmpIdAndCustomId",empCustomRelation);
	}
	
	/**
	 * 根据客户ID获取 员工客户关系
	 * @param customId
	 * @return
	 */
	public List<EmpCustomRelation>  getEmpCustomRelationByCustomId(String customId){
		
		List<EmpCustomRelation> empCustomRelationlList = super.getSqlSession().selectList("EmpCustomRelation.getEmpCustomRelationByCustomId",customId);
		return empCustomRelationlList;

	}
	/**
	 * 修改客户关系表客户ID
	 */
	@Override
	public void updateEmpCustomRelationCusId(Customer customer) throws SQLException {
		this.getSqlSession().update("EmpCustomRelation.updateEmpCustomRelationCusId",customer);
	}
	@Override
	public EmpCustomRelation getEmpCustomRelationByCondition(
			EmpCustomRelation empCustomRelation) {
		List<EmpCustomRelation> list = super.getSqlSession().selectList("EmpCustomRelation.getEmpCustomRelationByCondition", empCustomRelation);
		if(null != list && list.size() > 0) {
			
			return list.get(0);
		}
		return null;
	}
	@Override
	public List<EmpCustomRelation> getEmpCustomRelationListByCondition(
			EmpCustomRelation empCustomRelation) {
		
		return super.getSqlSession().selectList("EmpCustomRelation.getEmpCustomRelationListByCondition", empCustomRelation);
	}
	
	/**
	 * 用员工id和产品code查询 客户关系表记录
	 */
	@Override
	public List<EmpCustomRelation> getEmpCustomRelationByCusIdAndProductCode (Customer customer) {
		return this.getSqlSession().selectList("EmpCustomRelation.getEmpCustomRelationByCusIdAndProductCode", customer);
	}
	
	/**
	 * 用员工id和产品code查询 客户关系表记录
	 */
	@Override
	public List<EmpCustomRelation> getEmpCustomRelationByCusId(Customer customer) {
		return this.getSqlSession().selectList("EmpCustomRelation.getEmpCustomRelationByCusId", customer);
	}
	
	@Override
	public List<String> getBuyProduct(EmpCustomRelation empCustomRelation) {

		return super.getSqlSession().selectList("EmpCustomRelation.getBuyProduct", empCustomRelation);
	}
	
	
	public void updateServiceDate(EmpCustomRelation empCustomRelation){
		super.getSqlSession().update("EmpCustomRelation.updateServiceDate", empCustomRelation);
	}
	
	public List<EmpCustomRelation> getUnReceiveCustom(){
		List<EmpCustomRelation> empCustomRelationList = super.getSqlSession().selectList("EmpCustomRelation.getUnReceiveCustom");
		return empCustomRelationList;
	}
	
	/**
	 * 关系表ID修改关系表
	 */
	@Override
	public void updateEmpCustomRelationByEcrId(EmpCustomRelation empCustomRelation) {
		this.getSqlSession().update("EmpCustomRelation.updateEmpCustomRelationByEcrId",empCustomRelation);
	}
	
	/**
	 * 根据推送历史ID获取关系表记录
	 */
	@Override
	public EmpCustomRelation getEcrByPushHistIdAndPusherId(CustomStateHist csh) {
		return this.getSqlSession().selectOne("EmpCustomRelation.getEcrByPushHistIdAndPusherId",csh);
	}
	
	/**
	 * 根据客户ID删除关系表中的记录
	 */
	@Override
	public void delEmpCustomRelationByCusId(String cusId) {
		this.getSqlSession().delete("EmpCustomRelation.delEmpCustomRelationByCusId", cusId);
	}
	
	/**
	 * 用员工id和产品code查询 和客户类型 客户关系表记录
	 */
	@Override
	public List<EmpCustomRelation> getEmpCustomRelationByCusIdAndProductCodeAndCustomType(Customer customer) {
		return this.getSqlSession().selectList("EmpCustomRelation.getEmpCustomRelationByCusIdAndProductCodeAndCustomType", customer);
	}

}
